<%@ page import="com.scalar.chiptrack.utils.ConfigManager"%>
<%@ page import="com.scalar.chiptrack.users.Functionality"%>
<%@ page import="com.scalar.chiptrack.users.User"%>

<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-nested.tld" prefix="nested" %>
<%@ taglib uri="/WEB-INF/chiptrack-tags.tld" prefix="chiptrack" %>

<chiptrack:checkLogon sourcePage="/userNotification.do"/>
<%String chipTracContext = "/" + ConfigManager.getChipTracContext(); %>
<HTML>
<HEAD>
    <TITLE>Scalarsoft - ChipTrac</TITLE>
    <META http-equiv=Content-Type content="text/html; charset=windows-1252">
    <LINK href="<%=chipTracContext%>/jsp/includes/stylesheets/scalarsoft.css" type='text/css' rel='stylesheet'>
    <script src="<%=chipTracContext%>/jsp/includes/scripts/functions.js"></script>
    <script src="<%=chipTracContext%>/jsp/includes/scripts/validations.js"></script>

    <script language="javascript">

        function loadUserNotificationDetails()
        {
            var form = document.notificationForm;
            if ( form.userId.options[ form.userId.selectedIndex ].value == "")
            {
                form.action.value = "load";
            }
            else
            {
                form.action.value = "loadUserNotificationDetails";
            }
            form.submit();
        }

        function save()
        {
            var form = document.notificationForm;
            var errors = "";

            if ( form.userId.options[ form.userId.selectedIndex ].value == "" )
            {
                errors = errors + '<li> Please Select valid User Id';
            }

            if ( errors != "" )
            {
                var pa = document.getElementById("errors");
                pa.innerHTML = errors;
                return;
            }

            form.action.value="save";
            form.submit();
        }

        function allChkBoxValidation( checkBoxObj )
        {
            var form = document.notificationForm;
            var processTypeChkBoxObj    = form.process_type;
            var vendorsChkBoxObj        = form.hidden_vendor;

            for( var x = 0 ; x < processTypeChkBoxObj.length ; x++ )
            {
                if( checkBoxObj.checked == true )
                {
                    processTypeChkBoxObj[x].checked = true;
                }
                else
                {
                    processTypeChkBoxObj[x].checked = false;
                }
            }

            for( var x = 0 ; x < vendorsChkBoxObj.length ; x++ )
            {
                if( checkBoxObj.checked == true )
                {
                    document.notificationForm[ 'vendors[' + x + '].selected'].checked = true;
                }
                else
                {
                    document.notificationForm[ 'vendors[' + x + '].selected'].checked = false;
                }
            }

        }
        function baseProcessTypeChkBoxValidation( checkBoxObj, checkBoxObjValue )
        {
            var form = document.notificationForm;
            var processTypeChkBoxObj;

            processTypeChkBoxObj = form.process_type;

            if( checkBoxObj.checked == false )
            {
                form.allProcess.checked = false
            }

            if ( typeof  processTypeChkBoxObj.length == "undefined" )
            {
                processTypeChkBoxObj.checked = false
            }
            else
            {
                for( var x = 0 ; x < processTypeChkBoxObj.length ; x++ )
		        {
                    if( processTypeChkBoxObj[x].value == checkBoxObjValue )
                    {
                        processTypeChkBoxObj[x].checked = false
                    }
                }
            }
            /*var obj;
            for( var x = 0 ; x < form.process_type.length ; x++ )
		    {
                if( form.process_type[x].value == checkBoxObjValue )
                {
                    obj = form.process_type[x];
                }
            }
            selectDeselect ( obj );*/
        }

        function vendorChkBoxValidation( checkBoxObj )
        {
            var form = document.notificationForm;
            var selectedCheckBoxObj;
            var selectedProcessTypeValue;
            var errors = "";

            selectedProcessTypeValue = checkBoxObj.value;

            if( checkBoxObj.checked == false )
            {
                form.allProcess.checked = false
            }

            //selectedCheckBoxObj = eval( document.notificationForm[''+ checkBoxObj.value +''] );
            selectedCheckBoxObj = form.hidden_vendor;

            if ( typeof  selectedCheckBoxObj.length == "undefined" )
            {
                if( selectedCheckBoxObj[0].value == selectedProcessTypeValue )
                {
                    if( checkBoxObj.checked == true )
                    {
                        document.notificationForm[ 'vendors[0].selected'].checked = true;
                    }
                    else
                    {
                        document.notificationForm[ 'vendors[0].selected'].checked = false;
                    }
                }
            }
            else
            {
                for( var x = 0 ; x < selectedCheckBoxObj.length ; x++ )
		        {
                    if( selectedCheckBoxObj[x].value == selectedProcessTypeValue )
                    {
                        if( checkBoxObj.checked == true )
                        {
                            document.notificationForm[ 'vendors[' + x + '].selected'].checked = true;
                        }
                        else
                        {
                            document.notificationForm[ 'vendors[' + x + '].selected'].checked = false;
                        }
                    }
                }
            }
        }

        function reset()
        {
            document.notificationForm.reset();
        }
    </script>
</HEAD>
<BODY bgColor='#ffffff' leftMargin='0' topMargin='0' marginheight='0' marginwidth='0'>
<!-- Top Header Start-->
<%@ include file="/jsp/includes/header.jsp" %>
<!-- Top Header End-->
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
    <TR>
        <TD width='180' valign='top' background='<%=chipTracContext%>/jsp/includes/images/left_red_bg.gif'>
            <!-- Side Navigation start -->
            <%@ include file="/jsp/admin/admin_menu.jsp" %>
            <!-- end of the side navigations -->
        </TD>
        <TD bgcolor="#ffffff"><IMG height=9 src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" width=8></TD>
        <TD bgcolor="#ffffff"><IMG height=480 src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" width=2></TD>
        <TD valign="top" width="100%">
            <IMG height=1 src="<%=chipTracContext%>/jsp/includes/images/clear.gif" width=500 border=0>
            <BR>
            <html:form  action="/userNotificatonSetup" name="notificationForm" type="com.scalar.chiptrack.users.notification.NotificationForm" >
                <TABLE border="0" width="90%" align="center">
                    <input type="hidden" name="action" value="<%= (String)request.getAttribute("action")%>">
                    <TR>
                        <TD class="NArialW" colspan="5" align="center">User Notifications Setup</TD>
                    </TR>
                    <TR><TD class="NArialW" colspan="5" align="center">&nbsp;</TD></TR>
                    <TR>
                        <TD colspan="5">
                            <chiptrack:info styleClass="NArialL" />
                            <chiptrack:errors styleClass="NArialL" />
                            <chiptrack:messages type="info" styleClass="NArialL" />
                        </TD>
                    </TR>
                    <TR>
                        <TD colspan="5">
                            <TABLE border="0" width="100%" cellpadding="1" cellspacing="5">
                                <TR>
                                    <TD width="25%" class="NArialL">System User</TD>
                                    <TD width="75%" class="NArialL" colspan="6">
                                        <html:select property="userId"  name="notificationForm" styleClass="txfield" onchange="loadUserNotificationDetails()">
                                            <html:option value="">Select User</html:option>
                                            <html:options collection="userList" property="value" labelProperty="label"/>
                                        </html:select>
                                    </TD>
                                </TR>
                                <logic:equal parameter="action" value="loadUserNotificationDetails">
                                <TR>
                                    <TD width="25%" class="NArialL">Email</TD>
                                    <TD width="75%" class="NArialL" colspan="6">
                                        <html:text styleClass="txfield"  name="notificationForm" property="emailId" size="50" disabled="true" />&nbsp;&nbsp;
                                    </TD>
                                </TR>
                                <TR>
                                    <TD width="25%" class="NArialL">Type of Notifications</TD>
                                    <TD width="70%" class="NArialL" colspan="5">
                                        <nested:iterate indexId="sizesIdx" name="notificationForm" property="selectedNotifications" type="com.scalar.chiptrack.users.notification.Notification">
                                            <nested:checkbox property="selected" styleClass="txfield" />&nbsp;<nested:write property="notificationName" />
                                            <nested:hidden property="notificationId" ></nested:hidden>
                                            <BR>
                                        </nested:iterate>
                                    </TD>
                                    <TD colspan="1" width="5%">&nbsp;</TD>
                                </TR>
                                <TR>
                                    <TD colspan="7" class="NArialL" width="100%">Notifications Constraints</TD>
                                </TR>
                                <TR>
                                    <TD class="NArialL" width="20%">&nbsp;</TD>
                                    <TD width="80%" class="NArialL" colspan="6">
                                        <TABLE border="0" width="100%" cellpadding="1" cellspacing="1">
                                            <TR>
                                                <TD width="100%" class="NArialL" colspan="3">
                                                    <input type="checkbox" name="allProcess" onclick="javascript:allChkBoxValidation(this);" class="txfield">&nbsp;All Process
                                                </TD>
                                            </TR>
                                            <% String javaScriptFun = ""; %>
                                            <logic:iterate indexId="vendorHiddenIdx" id="validProcessType" name="allProcessTypes" type="java.lang.String">
                                            <TR>
                                                <TD class="NArialL" width="5%">&nbsp;</TD>
                                                <TD class="NArialL" width="85%" valign="top" align="left">
                                                    <input type="checkbox" name="process_type" value="<%= validProcessType %>" onclick="javascript:vendorChkBoxValidation(this);" class="txfield">
                                                    &nbsp;All&nbsp;<bean:write name="validProcessType" />&nbsp;Vendors

                                                </TD>
                                            </TR>
                                            <TR>
                                                <TD class="NArialL" width="5%">&nbsp;</TD>
                                                <TD class="NArialL" width="85%" valign="top" align="left">
                                                    <TABLE border="0" width="100%">
                                                        <nested:iterate indexId="sizeIdx" id="varNotification" name="notificationForm" property="vendors" type="com.scalar.chiptrack.users.notification.Notification">
                                                        <nested:equal property="processType" value="<%= validProcessType %>">
                                                        <TR>
                                                            <TD class="NArialL" width="5%">&nbsp;</TD>
                                                            <TD class="NArialL" width="95%">
                                                                <% javaScriptFun = "javascript:baseProcessTypeChkBoxValidation( this, '"+ validProcessType + "')"; %>
                                                                <nested:checkbox property="selected" styleClass="txfield" onclick="<%=javaScriptFun%>" />&nbsp;<nested:write property="vendorName" />
                                                                <nested:hidden property="locationId" ></nested:hidden>
                                                                <input type="hidden" name="hidden_vendor" value="<%= validProcessType %>" class="txfield">
                                                            </TD>
                                                        </TR>
                                                        </nested:equal>
                                                        </nested:iterate>
                                                    </TABLE>
                                                </TD>
                                            </TR>
                                            </logic:iterate>
                                       </TABLE>
                                    </TD>
                                </TR>
                                </logic:equal>
                            </TABLE>
                        </TD>
                    </TR>
                    <TR><TD colspan="5">&nbsp;</TD></TR>
                    <logic:equal parameter="action" value="loadUserNotificationDetails">
                    <TR>
                        <TD colspan="5" align="center">
                            <chiptrack:restrictedLink functionality='<%=Functionality.NOTIFICATION_SETUP %>' action='<%=User.USER_ACTION_MODIFY%>' href='javascript:save();' onmouseover="window.status='Click here to Save User Notifications and Vendors Access Details';return true" onmouseout="window.status='';return true">
                                <img border="0" src="<%=chipTracContext%>/jsp/includes/images/save.gif" title="Click here to Save User Notifications and Vendors Access Details"/>
                            </chiptrack:restrictedLink>
                            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                            <a href="javascript:reset()"><img border="0" src="<%=chipTracContext%>/jsp/includes/images/reset.gif" width="53" height="17" onmouseover="window.status='Click to Reset';return true" onmouseout="window.status='';return true" title="Click here to Reset"></a>
                        </TD>
                    </TR>
                    </logic:equal>
                </TABLE>
            </html:form>
            <BR><BR><BR>
        </TD>
        <TD bgcolor="#ffffff"><IMG height=1 src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" width=1></TD>
    </TR>
</TABLE>
<!-- Footer starts here -->
<%@ include file="/jsp/includes/footer.jsp" %>
<!-- footer end here -->

</BODY>
</HTML>
